package cn.lingyangwl.agile.pay.common.service.impl;

import cn.lingyangwl.agile.system.SystemApi;
import cn.lingyangwl.agile.system.model.rqrs.user.UserResp;
import cn.lingyangwl.agile.pay.common.constants.PayNodeEnum;
import cn.lingyangwl.agile.pay.common.manager.MchInfoManager;
import cn.lingyangwl.agile.pay.common.manager.PayOrderManager;
import cn.lingyangwl.agile.pay.common.mapper.PayNodeMapper;
import cn.lingyangwl.agile.pay.common.service.PayNodeService;
import cn.lingyangwl.agile.pay.constant.IfCodeEnum;
import cn.lingyangwl.agile.pay.model.entity.PayNode;
import cn.lingyangwl.agile.pay.model.entity.PayOrder;
import cn.lingyangwl.agile.pay.model.rqrs.PayNodeRS;
import cn.lingyangwl.framework.tool.core.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yitter.idgen.YitIdHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author shenguangyang
 */
@Service
public class PayNodeServiceImpl extends ServiceImpl<PayNodeMapper, PayNode> implements PayNodeService {
    @Resource
    private PayOrderManager payOrderManager;
    @Resource
    private SystemApi systemApi;
    @Resource
    private MchInfoManager mchInfoManager;

    @Override
    public List<PayNodeRS> listPayNode(String payOrderNo) {
        PayOrder payOrder = payOrderManager.getByPayOrderNo(payOrderNo);
        if (Objects.isNull(payOrder)) {
            return Collections.emptyList();
        }

        IfCodeEnum ifCodeEnum = IfCodeEnum.ofByCode(payOrder.getIfCode(), false);
        LambdaQueryWrapper<PayNode> lqw = PayNode.lqw().eq(PayNode::getPayOrderNo, payOrderNo)
                .orderByDesc(PayNode::getCreatedAt);
        List<PayNode> list = this.list(lqw);

        // 如果支付节点中的用户id不为空, 则以其为准, 否则以商户绑定的用户id为准
        Set<Long> userIds = list.stream().map(PayNode::getCreatedUid)
                .filter(Objects::nonNull).collect(Collectors.toSet());
        userIds.addAll(list.stream().map(PayNode::getCreatedUid).collect(Collectors.toSet()));
        Map<Long, String> groupById = systemApi.listUserByIds(new ArrayList<>(userIds)).stream()
                .collect(Collectors.toMap(UserResp::getId, UserResp::getUserName));
        return list.stream().map(e -> {
            String userName = groupById.getOrDefault(e.getCreatedUid(), "未知");
            return PayNodeRS.builder().createdAt(e.getCreatedAt()).id(e.getId())
                    .title(PayNodeEnum.of(e.getTitleCode()).parse(userName, ifCodeEnum)).build();
        }).collect(Collectors.toList());
    }

    @Override
    public void savePayNode(PayNode payNode) {
        if (Objects.isNull(payNode) || StringUtils.isEmpty(payNode.getPayOrderNo()) || StringUtils.isEmpty(payNode.getTitleCode())) {
            return;
        }
        payNode.setId(YitIdHelper.nextId());

        this.save(payNode);
    }
}
